<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Models\SalesRecordModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid\Filter;

class SalesRecordController extends AdminController
{
    protected $title = '销售记录';
    protected function grid(): NGrid
    {
        $grid = new NGrid(new SalesRecordModel());
        $grid->model()->leftJoin('shop_user as to_user', 'to_user.user_id', 'sales_record.to_user_id')
            ->leftJoin('shop_user as from_user', 'from_user.user_id', 'sales_record.from_user_id')
            ->select('sales_record.*', 'to_user.user_name as to_username', 'from_user.user_name as from_username')->orderByDesc('sales_record.created_at');
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('serial_number', __('编号'));
                $filter->equal('status', __('l.status'))->select(__('sales_record.statusArr'));
                $filter->equal('to_user-user_name', __('sales_record.to_user_id'));
                $filter->equal('from_user-user_name', __('sales_record.from_user_id'));
            });
        });
        $grid->column('id', 'ID')->sortable();
        $grid->column('serial_number', __('l.serialNumber'));
        $grid->column('type', __('sales_record.type'))->using(['order' => '普通订单', 'invite' => '邀请']);
        $grid->column('object_type', __('sales_record.object_type'))->using(['shop_order' => '普通订单']);
        $grid->column('to_user_id', __('sales_record.to_user_id'))->display(function () {
            return $this->to_username;
        });
        $grid->column('from_user_id', __('sales_record.from_user_id'))->display(function () {
            return $this->from_username;
        });
        $grid->column('status', __('l.status'))->using(['0' => '待入账', '1' => '已入账']);
        $grid->column('ratio', __('sales_record.ratio'));
        $grid->column('level', __('sales_record.level'));
        $grid->column('amount', __('sales_record.amount'));
        $grid->column('remark', __('l.remark'));
        $grid->column('extend', __('l.extend'));
        $grid->column('created_at', __('l.createdAt'));
        $grid->actions(function ($action) {
            $action->disableEdit();
            $action->disableDelete();
        });
        $grid->disableCreateButton();
        $grid->disableActions(false);
        return $grid;
    }
}
